﻿#include "WRWorkDeviceListModel.h"
#include "../WRSoft-Working-Common/WRWorkDeviceUuids.h"
#include "../WRSoft-Working-Common/WRWorkDeviceStatusChangeEvent.h"

#include <QCoreApplication>

//初始化工作设备列表项目。
WRWorkDeviceListEntry::WRWorkDeviceListEntry(int index, WRWorkDevice* target, WRWorkDeviceListModel* parent)
{
    m_index = index;
    m_status = WRWorkStatus::Unavail;
    m_target = target;
    m_parent = parent;
    m_uuid = target->uuid().toString();
    m_switchDealer = target->queryTaskDealer(WRWorkSwitchDealer);
    m_statusSource = target->queryStateSource(WRWorkStatusSource);
}
//清理当前对象所占用的相关资源。
WRWorkDeviceListEntry::~WRWorkDeviceListEntry(void)
{
    //add code here
}

//设置为活动。
void WRWorkDeviceListEntry::activate(void)
{
    m_statusSource->addHandler(this);
}
//设置为不活动。
void WRWorkDeviceListEntry::inactivate(void)
{
    m_statusSource->removeHandler(this);
}

//当状态改变后调用此方法。
void WRWorkDeviceListEntry::doProcess(const void* value)
{
    QCoreApplication::postEvent(m_parent, new WRWorkDeviceStatusChangeEvent(m_index, m_target, *(const WRWorkStatus*)value));
}

//初始化工作设备列表模型。
WRWorkDeviceListModel::WRWorkDeviceListModel(WRWorkDriver* driver)
    : m_list(driver->count())
{
    for(WRu i = 0, l = m_list.count(); i < l; ++i)
    {
        WRWorkDeviceListEntry* item = new WRWorkDeviceListEntry((WR32s)i, driver->index(i), this);
        m_list[i] = item;
    }
}
//清理当前对象所占用的相关资源。
WRWorkDeviceListModel::~WRWorkDeviceListModel(void)
{
    for(WRu i = 0, l = m_list.count(); i < l; ++i)
    {
        WRWorkDeviceListEntry* item = m_list[i];
        delete item;
    }
}

//设置为活动。
void WRWorkDeviceListModel::activate(void)
{
    for(WRu i = 0, l = m_list.count(); i < l; ++i)
    {
        m_list[i]->activate();
    }
}
//设置为不活动。
void WRWorkDeviceListModel::inactivate(void)
{
    for(WRu i = 0, l = m_list.count(); i < l; ++i)
    {
        m_list[i]->inactivate();
    }
}

//处理事件。
bool WRWorkDeviceListModel::event(QEvent *e)
{
    if (WRWorkDeviceStatusChangeEvent::identify(e))
    {
        WRWorkDeviceStatusChangeEvent* e2 = static_cast<WRWorkDeviceStatusChangeEvent*>(e);
        WRWorkDeviceListEntry* item = m_list[e2->index()];
        if (item->status(e2->status()))
        {
            QModelIndex idx = createIndex(e2->index(), 0);
            emit dataChanged(idx, idx);
        }
        return true;
    }
    return QAbstractItemModel::event(e);
}

//判断是否存在索引。
bool WRWorkDeviceListModel::hasIndex(int row, int column, const QModelIndex &parent)
{
    if (parent.isValid())
    {
        return false;
    }
    return row >= 0 && row < (int)(m_list.count()) && column >= 0 && column < 4;
}

//获取索引。
QModelIndex WRWorkDeviceListModel::index(int row, int column, const QModelIndex &parent) const
{
    if (parent.isValid())
    {
        return QModelIndex();
    }
    return createIndex(row, column);
}

//获取父索引。
QModelIndex WRWorkDeviceListModel::parent(const QModelIndex &) const
{
    return QModelIndex();
}

//获取行数量。
int WRWorkDeviceListModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
    {
        return 0;
    }
    return (int)m_list.count();
}
//获取列数量。
int WRWorkDeviceListModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
    {
        return 0;
    }
    return 4;
}

//判断是否具有子级。
bool WRWorkDeviceListModel::hasChildren(const QModelIndex & parent) const
{
    if (parent.isValid())
    {
        return false;
    }
    return true;
}

//获取内容数据。
QVariant WRWorkDeviceListModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole)
    {
        const WRWorkDeviceListEntry* item = m_list[index.row()];
        switch (index.column()) {
        case 0:
            return item->statusString();
        case 1:
            return item->uuidString();
        case 2:
            return item->title();
        case 3:
            return item->description();
        default:
            return QVariant();
        }
    }
    return QVariant();
}
//获取标题数据。
QVariant WRWorkDeviceListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole)
    {
        if (orientation == Qt::Horizontal)
        {
            static QString statusString = QStringLiteral("状态");
            static QString uuidString = QStringLiteral("标识");
            static QString titleString = QStringLiteral("名称");
            static QString descriptionString = QStringLiteral("描述");

            switch (section) {
            case 0:
                return statusString ;
            case 1:
                return uuidString;
            case 2:
                return titleString;
            case 3:
                return descriptionString;
            default:
                return QVariant();
            }
        }
    }
    return QVariant();
}
